# coding: utf-8

from numpy import array, inf
from .oneway import find_correspondence, reject_correspondence


def find_correspondences(source_cloud, target_cloud, max_dist=inf):
    source_correspondences = list(map(
            # find closest point.
            find_correspondence(target_cloud),
            source_cloud
    ))

    target_correspondences = list(map(
            # find closest point.
            find_correspondence(source_cloud),
            target_cloud
    ))

    correspondences = list(filter(
            lambda c: all(c[0] == target_correspondences[c[2]][1]),
            source_correspondences
    ))

    print(">>> correspondences: %d" % len(correspondences))
    # if len(correspondences) / len(source_cloud) < 0.8:
    #     correspondences = source_correspondences

    return [array(p) for p in zip(*map(
            lambda c: c[:2], filter(reject_correspondence(max_dist),
                                    correspondences)
    ))]
